package com.example.demo.listener;

import com.example.demo.config.Constants;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.Charset;

/**
 * 路由器模式监听
 *
 * @param
 * @return
 * @date 2024/11/4 15:07
 * @author lililiang
 */
@Slf4j
@Component
public class RoutingListener {
    @RabbitHandler
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(value = Constants.QUEUE_ROUTING1),
                    exchange = @Exchange(value = Constants.EXCHANGE_ROUTING),
                    key = {Constants.KEY_ROUTING1}
            )
    })
    public void onMessage1(Message message, Channel channel) throws IOException {
        String strReceive = new String(message.getBody(), Charset.defaultCharset());
        log.info("queue-routing1监听到数据:" + strReceive);
    }

    @RabbitHandler
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(value = Constants.QUEUE_ROUTING2),
                    exchange = @Exchange(value = Constants.EXCHANGE_ROUTING),
                    key = {Constants.KEY_ROUTING21, Constants.KEY_ROUTING22}
            )
    })
    public void onMessage2(Message message, Channel channel) throws IOException {
        String strReceive = new String(message.getBody(), Charset.defaultCharset());
        log.info("queue-routing2监听到数据:" + strReceive);
    }
}
